package com.developer.platform.domain.scheduler;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * 把普通pojo实例化到spring容器中，相当于配置文件中的
 *
 * @author ：qiaoliang
 * @date ：2021-01-13
 */
@Component
public class SchedulerFactory extends AdaptableJobFactory {

  private final AutowireCapableBeanFactory capableBeanFactory;

  public SchedulerFactory(
      AutowireCapableBeanFactory capableBeanFactory) {
    this.capableBeanFactory = capableBeanFactory;
  }

  @Override
  protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
    // 获取到需要注入的class
    Class<?> clazz = bundle.getJobDetail().getJobClass();
    // 进行注入
    return capableBeanFactory.autowire(clazz, 4, true);
  }

}
